package com.godyao.mall.pms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.godyao.mall.pms.entity.AttrEntity;
import com.godyao.mall.pms.service.CategoryService;
import com.godyao.mall.pms.vo.request.AttrGroupRequestVo;
import com.godyao.mall.pms.vo.response.AttrGroupResponseVo;
import com.godyao.mall.pms.vo.response.AttrResponseVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import com.godyao.mall.pms.entity.AttrGroupEntity;
import com.godyao.mall.pms.service.AttrGroupService;
import com.godyao.mall.core.utils.PageUtils;
import com.godyao.mall.core.model.ResultMsg;



/**
 * 属性分组
 *
 * @author godyao
 * @email godyao0801@qq.com
 * @date 2022-04-19 14:42:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("pms/attrgroup")
public class AttrGroupController {

    private final AttrGroupService attrGroupService;

    private final CategoryService categoryService;

    /**
     * 获取分类属性分组
     */
    @GetMapping("/list/{catlogId}")
    public ResultMsg list(@RequestParam Map<String, Object> params, @PathVariable(name = "catlogId") Long catlogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catlogId);

        return ResultMsg.resultSuccess(page);
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public ResultMsg attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.getNoRelationAttr(params,attrgroupId);
        return ResultMsg.resultSuccess(page);
    }

    /**
     * 获取分组下的属性
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public ResultMsg listAttrByGroup(@PathVariable Long attrGroupId){
        List<AttrEntity> attrResponseVoList = attrGroupService.listAttrByGroup(attrGroupId);

        return ResultMsg.resultSuccess(attrResponseVoList);
    }

    /**
     * 获取分组下的属性
     */
    @PostMapping("/attr/relation")
    public ResultMsg recordRelation(@RequestBody List<AttrGroupRequestVo> requestVos){
        attrGroupService.saveBatch(requestVos);

        return ResultMsg.resultSuccess("OK");
    }

    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public ResultMsg getAttrGroupByCateId(@PathVariable Long catelogId){
        List<AttrGroupResponseVo> res = attrGroupService.getAttrGroupByCateId(catelogId);
        return ResultMsg.resultSuccess(res);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public ResultMsg info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] path = categoryService.getPathById(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);
        return ResultMsg.resultSuccess(attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResultMsg save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return ResultMsg.resultSuccess("OK");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResultMsg update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return ResultMsg.resultSuccess("OK");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResultMsg delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return ResultMsg.resultSuccess("OK");
    }

    /**
     * 关联删除
     */
    @PostMapping("/attr/relation/delete")
    public ResultMsg deleteRelation(@RequestBody AttrGroupRequestVo[] attrGroupIds){
		attrGroupService.removeRelations(attrGroupIds);

        return ResultMsg.resultSuccess("OK");
    }

}
